package controlComponents;

import java.awt.Dimension;
import java.util.LinkedList;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class PresenceViewer implements ScaleWriter {

	private JPanel threadPanel;
	private Icon icon;
	private double old = 0;
	private double nextData = 0;
	private String nextTime = new String();
	private int ID;
	private LinkedList<Information> list = new LinkedList<Information>();

	public PresenceViewer(int id) {
		this.ID = id;
		list = new LinkedList<Information>();
		icon = new ImageIcon("lib/Active.jpg");

	}

	@Override
	public void run() {
		Information i;
		while (list.size() > 0 || (hasFinished == false && list.size() < 1)) {
			while (list.size() < 1) {
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			i = list.poll();
			nextData = i.getData();
			nextTime = i.getTime();
			if (old != nextData) {
				System.out.println("new data " + nextData);
				old = nextData;
				threadPanel.removeAll();
				for (int j = 0; j < (int) nextData; j++) {
					JLabel label = new JLabel(icon);
					label.setToolTipText((int) nextData
							+ " people in room \n starting from " + nextTime);
					label.setBounds(j * 25 + 5, 5, 25, 25);
					label.setOpaque(true);
					threadPanel.add(label);
					threadPanel.setPreferredSize(new Dimension(
							(int) nextData * 30, size));
					threadPanel
							.setSize(new Dimension((int) nextData * 30, size));
					threadPanel.repaint();
				}

			}
		}

	}

	@Override
	public void writeNext(Information i) {
		list.add(i);
	}

	@Override
	public void setPanel(JPanel panel) {
		threadPanel = panel;

	}

	private boolean hasFinished = false;

	@Override
	public void finished() {
		hasFinished = true;
	}

}